#include <limits.h>
#define LM1 1
#define LM2 2
#define LM0 3
#define RM1 4
#define RM2 5
#define RM0 6

#define LS 7
#define MS 8
#define RS 9

#define count 10
#define maxadjust 50
#define base 128
float MMAX ;

float calculate(int val[],int times )
{
  int max = INT_MAX;
  int min = INT_MIN;

  for(int t = 0;t<times;t++)
  {
    if(val[t] > max)
    {
      max = ele[t];
    }
    if(val[t] < min)
    {
      min = val[t];
    }
    sum += val[t];
  }

float ava = sum-max-min/(float)(times-2);
return ava;

}
void SPEED(float LAVA,float MAVA,float RAVA)
{
int offset = LAVA - RAVA;
int bigsmall =  MMAX - MAVA;
int leftspeed = base - (sign(offset))*(abs(offset)*bigsmall*maxadjust)/(4095*4095);
int rightspeed = base + (sign(offset))*(abs(offset)*bigsmall*maxadjust)/(4095*4095);
  analogWrite(LM0, leftspeed);
  analogWrite(RM0, rightspeed);
  digitalWrite(LM1,HIGH);
  digitalWrite(LM2,LOW);
  digitalWrite(RM1,HIGH);
  digitalWrite(RM2,LOW);
}


void setup() 
{

 Serial.begin(115200);

 pinMode(LM1,OUTPUT);
 pinMode(LM2,OUTPUT);
 pinMode(LM0,OUTPUT);
 pinMode(RM1,OUTPUT);
 pinMode(RM2,OUTPUT);
 pinMode(RM0,OUTPUT);

 pinMode(LS,INPUT);
 pinMode(MS,INPUT);
 pinMode(RS,INPUT);

}

void loop() 
{
  int LS_Value[count];
  for(int t = 0;t<count;t++)
  {
    LS_Value[t] = analogRead(LS);
    delayMicroseconds(10);
  }
  int MS_Value[count];
  for(int t = 0;t<count;t++)
  {
    MS_Value[t] = analogRead(MS);
    delayMicroseconds(10);
  }
  int RS_Value[count];
  for(int t = 0;t<count;t++)
  {
    RS_Value[t] = analogRead(RS);
    delayMicroseconds(10);
  }

  float LAVA = calculate(LS_Value,count);
  float MAVA = calculate(MS_Value,count);
  float RAVA = calculate(RS_Value,count);
  
  SPEED(LAVA,MAVA,RAVA);

}


















